package com.soft.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.soft.entity.KanbanItem;

@Mapper
public interface KanbanItemMapper extends BaseMapper<KanbanItem> {
    
    /**
     * 获取看板的所有卡片，关联日程信息
     */
    @Select("SELECT " +
                        "ki.item_id   AS itemId, " +
                        "ki.kanban_id AS kanbanId, " +
                        "ki.schedule_id AS scheduleId, " +
                        "ki.column_id AS columnId, " +
                        "ki.assignee_id AS assigneeId, " +
                        "ki.assign_time AS assignTime, " +
                        "ki.sort_order AS sortOrder, " +
            "s.title AS schedule_title, s.title AS title, s.status AS schedule_status, s.priority AS schedule_priority, " +
                        "u.nickname AS assignee_name " +
                        "FROM kanban_item ki " +
                        "LEFT JOIN schedule s ON ki.schedule_id = s.schedule_id " +
                        "LEFT JOIN user u ON ki.assignee_id = u.user_id " +
                        "WHERE ki.kanban_id = #{kanbanId} " +
                        "ORDER BY ki.column_id, ki.sort_order")
    List<Map<String, Object>> getItemsWithSchedule(Integer kanbanId);
    
    /**
     * 移动卡片时更新其他卡片的排序
     */
    @Update("UPDATE kanban_item SET sort_order = sort_order + 1 " +
            "WHERE column_id = #{columnId} AND sort_order >= #{sortOrder}")
    void updateSortOrdersAfterMove(Integer columnId, Integer sortOrder);

    /**
     * 根据 schedule_id 更新日程状态
     */
    @Update("UPDATE schedule SET status = #{status} WHERE schedule_id = #{scheduleId}")
    void updateScheduleStatus(Integer scheduleId, String status);
}
